<template>
  <div class="modal" v-show="$store.state.isOpenlogin">
    <div class="modal_bg" @click="closeLogin"></div>
    <div class="modal_content">
      <img
        class="close"
        src="../assets/images/login/userArrow.png"
        @click="closeLogin"
        alt=""
      />
      <section>
        <span :class="{ active: weixin }" @click="weixin = true"
          >手机号登录</span
        >
        <span :class="{ active: !weixin }" @click="weixinLogin"
          >微信扫码登录</span
        >
      </section>
      <div class="phoneLogin" v-show="weixin">
        <div class="tanxin line">
          <input
            placeholder="请输入手机号"
            maxlength="11"
            type="text"
            v-model="phonetxt"
          />
        </div>
        <!-- 验证码 -->
        <div class="verify_box">
          <slide-verify
            :l="42"
            :r="10"
            :w="360"
            :h="155"
            slider-text="向右滑动"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
        </div>
        <div class="tanxin line">
          <input
            placeholder="请输入验证码"
            type="text"
            maxlength="4"
            v-model="codetxt"
          />
          <div class="btn" v-if="isNumber" @click="huoqu">获取验证码</div>
          <div class="btn1" v-else>{{ num }}</div>
        </div>
        <div class="btn_login" @click="getphone">登录</div>
      </div>

      <div class="wxLogin" v-show="!weixin"></div>
    </div>
  </div>
</template>

<script>
const jiaoyan = (phonetxt) => {
  let reg =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  return reg.test(phonetxt.trim());
};
import { GetCodeapi, PostCodeapi } from "@/request/api";
export default {
  data() {
    return {
      //微信切换
      weixin: true,
      jiaoyanmsg: "",
      phonetxt: "",
      codetxt: "",
      code: 1111,
      alertMsg: {
        isAlert: true,
        isMsg: "",
        icon: "",
      },
      isNumber: true,
      num: 60,
    };
  },
  methods: {
    //微信
    weixinLogin() {
      this.weixin = false;
    },
    //滑块
    closeLogin() {
      this.$store.commit("chageisOpenLogin", false);
    },
    onSuccess() {
      this.msg = "成功";
      this.jiaoyanmsg = true;
    },
    onFail() {
      this.msg = "失败";
      this.jiaoyanmsg = false;
    },
    onRefresh() {
      this.msg = "";
    },
    //登录按钮的
    getphone() {
      let rest = jiaoyan(this.phonetxt);
      if (rest == "" || this.codetxt.trim() == "") {
        this.alertMsg.isMsg = "请输入手机号和验证码";
        this.alertMsg.icon = "info";
      } else if (this.codetxt.length !== 4) {
        this.alertMsg.isMsg = "请正确输入验证码";
        this.alertMsg.icon = "info";
      } else if (rest || this.codetxt == 4) {
        PostCodeapi({
          verifyCode: this.code,
          phone: this.phonetxt,
        }).then((res) => {
          if (res.data.code === 0) {
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            this.alertMsg.isMsg = "登录成功";
            this.alertMsg.icon = "success";
          }
          this.$store.commit("chageAlertmsg", this.alertMsg);
          this.$store.dispatch("Asyncalert", true);
        });
        this.$store.commit("chageisOpenLogin", false);
        this.$store.commit("chageisLogin", true);
      }
      this.$store.commit("chageAlertmsg", this.alertMsg);
      this.$store.dispatch("Asyncalert", true);
    },
    //获取验证码
    huoqu() {
      GetCodeapi({ phone: this.phonetxt }).then((res) => {
        if (res.data.code === 0) {
          this.alertMsg.isMsg = "获取成功";
          this.alertMsg.icon = "success";
          this.$store.commit("chageAlertmsg", this.alertMsg);
          this.isNumber = false;
          setInterval(() => {
            this.num--;
            if (this.num == 0) {
              (this.isNumber = true), (this.num = 60);
            }
          }, 1000);
        } else {
          this.alertMsg.isMsg = "获取失败";
          this.alertMsg.icon = "danger";
          this.$store.commit("chageAlertmsg", this.alertMsg);
          this.$store.dispatch("Asyncalert", true);
        }
      });
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";

.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .modal_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
  }
  .modal_content {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url(../assets/images/login/loginBg.png) no-repeat;
    .close {
      position: absolute;
      top: 20px;
      right: 60px;
    }
    section {
      display: flex;
      justify-content: center;
      padding-top: 50px;
      margin-bottom: 30px;
      span {
        color: #ccc;
        cursor: pointer;
        &:nth-of-type(1) {
          border-right: 1px solid #ccc;
          margin-right: 10px;
          padding-right: 10px;
        }
        &.active {
          color: #333;
        }
      }
    }
    .line {
      width: 360px;
      margin: 0 auto 20px;
      height: 50px;
      input {
        flex: 1;
        text-indent: 1em;
        outline: none;
      }
      .btn {
        width: 90px;
        height: 50px;
        text-align: center;
        background: @blue;
        color: white;
        cursor: pointer;
        line-height: 50px;
        margin-left: 10px;
      }
      .btn1 {
        width: 90px;
        height: 50px;
        text-align: center;
        background: #c5c4c4;
        color: white;
        cursor: pointer;
        line-height: 50px;
        margin-left: 10px;
      }
    }
    .btn_login {
      width: 360px;
      height: 50px;
      text-align: center;
      background: @blue;
      color: white;
      cursor: pointer;
      line-height: 50px;
      margin: auto;
      font-weight: 900;
      letter-spacing: 20px;
    }
  }
}
.verify_box {
  width: 360px;
  margin: 0 auto 20px;
  /deep/.slide-verify {
    width: 100% !important;
    canvas {
      position: absolute;
      top: -150px;
      left: 0;
      display: none;
      box-sizing: border-box;
    }
    .slide-verify-refresh-icon {
      position: absolute;
      top: -150px;
      display: none;
    }
    &:hover {
      canvas {
        display: block;
      }
      .slide-verify-refresh-icon {
        display: block;
      }
    }
  }
}

.wxLogin {
  width: 300px;
  height: 300px;
  background-color: skyblue;
  margin: 0 auto;
}
</style>